<?php
class Framework{		
	static public function run() {
		self::initDefine();
		self::initLoad();
		self::initConfig();
		self::initRouteName();
		self::initRoute();
	}
	
	static private function initDefine() {
		define('ROOT_PATH'       , getcwd());
		define('DS'              , DIRECTORY_SEPARATOR);		
		define('APP_PATH'        , ROOT_PATH      . DS . 'App'        . DS);
		define('FRAMEWORK_PATH'  , ROOT_PATH      . DS . 'Framework'  . DS);
		define('PUBLIC_PATH'     , ROOT_PATH      . DS . 'Public'     . DS);
		
		define('CONFIG_PATH'     , FRAMEWORK_PATH . 'Config'     . DS);
		define('CORE_PATH'       , FRAMEWORK_PATH . 'Core'       . DS);		
		define('DB_PATH'         , FRAMEWORK_PATH . 'Db'         . DS);
		define('LIB_PATH'        , FRAMEWORK_PATH . 'Lib'        . DS);
		define('LOG_PATH'        , PUBLIC_PATH    . 'Log'        . DS);
		define('CONTROLLER_PATH' , APP_PATH       . 'Controller' . DS);
		define('MODEL_PATH'      , APP_PATH       . 'Model'      . DS);
		define('__VIEW__'        , APP_PATH       . 'View'       . DS);
		define('VENDOR_PATH'     , FRAMEWORK_PATH . 'Compent'    . DS . 'vendor' . DS);
		
		session_start();
	}
	
	static private function initRouteName() {
		$platform   = isset($_REQUEST['p']) ? self::uc($_REQUEST['p']) : $GLOBALS['config']['APP']['D_P'];
		$controller = isset($_REQUEST['c']) ? self::uc($_REQUEST['c']) : $GLOBALS['config']['APP']['D_C'];
		$action     = isset($_REQUEST['a']) ? self::uc($_REQUEST['a']) : $GLOBALS['config']['APP']['D_A'];

		define('PLATFORM'   , $platform);
		define('CONTROLLER' , $controller);
		define('ACTION'     , $action);
	}
	
	static private function initLoad() {
		spl_autoload_register(__CLASS__ . '::autoLoad');
	}
	
	static private function autoLoad($className) {
		$class = array(
			'Conf'       => CORE_PATH . $className . '.class.php',
			'PdoClass'   => DB_PATH   . $className . '.class.php',
			'LogClass'   => LIB_PATH  . $className . '.class.php',
			'Controller' => CORE_PATH . $className . '.class.php',
			'Model'      => CORE_PATH . $className . '.class.php',
		);
		
		if(array_key_exists($className, $class)) {
			require $class[$className];
		} else if(substr($className, -10) == 'Controller') {
			require CONTROLLER_PATH . PLATFORM . DS . $className . '.class.php';
		} else if(substr($className, -5) == 'Model'){
			require MODEL_PATH      . $className . '.class.php';
		} else {
			require LIB_PATH        . $className . '.class.php';
		}
	}
	
	static private function initConfig() {
		$GLOBALS['config'] = require CONFIG_PATH . 'Config.php';
	}
	
	static private function initRoute() {
		$controllerName = CONTROLLER . 'Controller';
		$actionName     = ACTION     . 'ACTION';
		$controller     = new $controllerName();
		$controller -> $actionName();
	}
	
	static private function uc($data) {
		return ucfirst(strtolower($data));
	}
}
?>